package com.example.exception;


import org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public void handlerFileUploadSizeTooLarger(HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write("传文件大小超出限制！ ");
        //1.记录日志
        //2.发邮件，发短信系统管理员
        out.flush();
        out.close();
    }
    @ExceptionHandler(ArithmeticException.class)
    public void handlerArithmeticException(HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write("算术异常了......");
        out.flush();
        out.close();
    }

    @ExceptionHandler(Exception.class)
    public void globalHandler(HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write("服务器内部出错！ ");
        out.flush();
        out.close();
    }
}
